package com.hexinfo.dmpro.monitor.task;

import cn.hutool.core.date.DateUtil;
import com.hexinfo.dmpro.common.utils.StringUtils;
import com.hexinfo.dmpro.monitor.config.MonitorCache;
import com.hexinfo.dmpro.monitor.config.MonitorConfig;
import com.hexinfo.dmpro.monitor.model.base.MonitorReq;
import com.hexinfo.dmpro.monitor.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author peng
 * @date 2021/11/2 19:29
 */
@Service
@Slf4j
public class MonitorTask {

    @Autowired
    private MonitorConfig monitorConfig;

    @Autowired
    private YarnService yarnService;

    @Autowired
    private ImpalaService impalaService;

    @Autowired
    private HdfsService hdfsService;

    @Autowired
    private ClusterHostService clusterHostService;

    @Autowired
    private CmStateService cmStateService;

    @Autowired
    private CmImpalaService cmImpalaService;

    public void impalaAppTask(String time) {
        String dataTime = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss");
        List<MonitorReq> monitorReqs = MonitorCache.getClusters(monitorConfig.getNowCenterId());

        //查询6分钟前到现在的任务
        if(StringUtils.isBlank(time)){
            time = "6";
        }
        String startDate = LocalDateTime.now().plusHours(1).minusMinutes(Long.parseLong(time)).toString().substring(0, 19).replace("T", " ");;

        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setStartTime(startDate);
            monitorReq.setTaskTime(dataTime);
            try{
                impalaService.getCmApiImpalaApp(monitorReq, true);
            }catch (Exception e){
                log.error("获取impala任务失败", e);
            }
        }
    }

    /**
     * cm组件状态 监控
     */
    public void cmStateTask(){
        String dataTime = LocalDateTime.now().toString().substring(0, 19).replace("T", " ");
        List<MonitorReq> monitorReqs = MonitorCache.getClusters(monitorConfig.getNowCenterId());
        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setTaskTime(dataTime);
            try{
                cmStateService.getSate(monitorReq, true);
            }catch (Exception e){
                log.error("cmStateTask 任务失败", e);
            }
        }
    }

    public void getYarnMetrics() {
        String dataTime = LocalDateTime.now().toString().substring(0, 19).replace("T", " ");
        List<MonitorReq> monitorReqs = MonitorCache.getClusters(monitorConfig.getNowCenterId());
        String startDate = LocalDate.now().toString();
        String endDate = LocalDate.now().plusDays(1).toString();
        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setStartTime(startDate);
            monitorReq.setEndTime(endDate);
            monitorReq.setTaskTime(dataTime);
            try{
                yarnService.getYarnMetrics1(monitorReq, true);
            }catch (Exception e){
                log.error("getYarnMetrics 任务失败", e);
            }
        }
    }

    public void yarnAppTask(String time) {
        String dataTime = LocalDateTime.now().toString().substring(0, 19).replace("T", " ");
        //查询6分钟前到现在的任务
        time = "6";
        List<MonitorReq> monitorReqs = MonitorCache.getClusters(monitorConfig.getNowCenterId());
        String startDate = LocalDateTime.now().minusMinutes(Long.parseLong(time)).toString().substring(0, 19).replace("T", " ");

        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setStartTime(startDate);
            monitorReq.setTaskTime(dataTime);
            try{
                yarnService.getYarnApp1(monitorReq, true);
            }catch (Exception e){
                log.error("获取yarn任务失败", e);
            }
        }
    }

    public void yarnSchedulerTask() {
        String dataTime = LocalDateTime.now().toString().substring(0, 19).replace("T", " ");
        List<MonitorReq> monitorReqs = MonitorCache.getClusters(monitorConfig.getNowCenterId());
        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setTaskTime(dataTime);
            try{
                yarnService.getYarnScheduler(monitorReq, true);
            }catch (Exception e){
                log.error("kafkaGapTask 任务失败", e);
            }
        }
    }

    public void hdfsResourceTask() {
        String dataTime = LocalDateTime.now().toString().substring(0, 19).replace("T", " ");
        List<MonitorReq> monitorReqs = MonitorCache.getClusters(monitorConfig.getNowCenterId());
        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setTaskTime(dataTime);
            try{
                hdfsService.getHdfsResource(monitorReq, true);
            }catch (Exception e){
                log.error("kafkaGapTask 任务失败", e);
            }
        }
    }

    public void impalaPoolTask() {
        String dataTime = LocalDateTime.now().toString().substring(0, 19).replace("T", " ");
        List<MonitorReq> monitorReqs = MonitorCache.getClusters(monitorConfig.getNowCenterId());
        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setTaskTime(dataTime);
            try{
                cmImpalaService.getImpalaPool(monitorReq, true);
            }catch (Exception e){
                log.error("impala资源池 任务失败", e);
            }
        }
    }

    public void clusterHostTask() {
        String dataTime = LocalDateTime.now().toString().substring(0, 19).replace("T", " ");
        List<MonitorReq> monitorReqs = MonitorCache.getCms(monitorConfig.getNowCenterId());
        for (MonitorReq monitorReq : monitorReqs) {
            monitorReq.setTaskTime(dataTime);
            try{
                clusterHostService.getHostInfo(monitorReq, true);
            }catch (Exception e){
                log.error("kafkaGapTask 任务失败",e);
            }
        }
    }
}
